package org.yray.sip.server;


import org.yray.sip.factory.inet.INotify;
import org.yray.sip.rtp.data.MediaContext;
import org.yray.sip.rtp.server.UrtcpServerHandler;
import org.yray.sip.rtp.server.UrtpServerHandler;
import org.yray.sip.system.NoticeContext;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

/**
 * 存本服务IP及对应服务IP
 */
public class SipServerContext {

    private List<INotify> notifyList=new ArrayList<>();
    //连接的sip服务ID
    private String sipIp;
    //连接的sip服务端口
    private int sipPort;
    //本身sip服务运行的IP，如果为空则取本地IP
    private String runIp;
    //本身sip服务器运行的端口
    private int runPort;
    //默认运行端口号加10
    private int runRtpPort;
    //默认运行端口号加11
    private int runRtcpPort;
    private String filePath;

    public UrtpServerHandler getUrtpServerHandler() {
        return urtpServerHandler;
    }

    public UrtcpServerHandler getUrtcpServerHandler() {
        return urtcpServerHandler;
    }

    public SipServerHandler getSipServerHandler() {
        return sipServerHandler;
    }

    public void setSipServerHandler(SipServerHandler sipServerHandler) {
        this.sipServerHandler = sipServerHandler;
    }

    public void setUrtpServerHandler(UrtpServerHandler urtpServerHandler) {
        this.urtpServerHandler = urtpServerHandler;
    }

    public void setUrtcpServerHandler(UrtcpServerHandler urtcpServerHandler) {
        this.urtcpServerHandler = urtcpServerHandler;
    }

    private UrtpServerHandler urtpServerHandler;
    private UrtcpServerHandler urtcpServerHandler;
    private SipServerHandler sipServerHandler;
    private final SipExtTable sipExtTable;
    private final MediaContext mediaContext;

    public SipServerContext()
    {
        this.sipExtTable=new SipExtTable();
        this.mediaContext=new MediaContext();
    }

    public MediaContext getMediaContext() {
        return mediaContext;
    }

    public SipServerContext(String sipIp, int sipPort, String runIp, int runPort, String filePath)
    {
        this.sipIp = sipIp;
        this.sipPort = sipPort;
        this.runIp = runIp;
        this.runPort = runPort;
        this.runRtpPort=runPort+10;
        this.runRtcpPort=runPort+11;
        this.filePath = filePath;
        this.sipExtTable=new SipExtTable();
        this.mediaContext=new MediaContext();
        this.sipServerAddr=new InetSocketAddress(sipIp,sipPort);
    }


    public SipExtTable getSipExtTable() {
        return sipExtTable;
    }


    public void setSipIp(String sipIp) {
        this.sipIp = sipIp;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    private InetSocketAddress sipServerAddr;


    public String getRunIp() {
        return runIp;
    }

    public void setRunIp(String runIp) {
        this.runIp = runIp;
    }

    public int getRunPort() {
        return runPort;
    }

    public void setRunPort(int runPort) {
        this.runPort = runPort;
    }

    public InetSocketAddress getSipServerAddr() {
        return sipServerAddr;
    }

    public void setSipServerAddr(InetSocketAddress sipServerAddr) {
        this.sipServerAddr = sipServerAddr;
    }

    public String getSipIpPort()
    {
        return sipIp+":"+sipPort;
    }

    public String getSipIp() {
        return sipIp;
    }

    public int getSipPort() {
        return sipPort;
    }

    public void setSipPort(int sipPort) {
        this.sipPort = sipPort;
    }

    public int getRunRtpPort() {
        return runRtpPort;
    }

    public int getRunRtcpPort() {
        return runRtcpPort;
    }

    public void add(INotify notify)
    {
        notifyList.add(notify);
    }

    public void notice(NoticeContext noticeContext)
    {
        for(INotify notify:notifyList)
        {
            notify.notice(noticeContext);
        }
    }
}
